﻿using System;
using System.Threading;
using System.Windows.Forms;

namespace MutsiEngine
{
    class GameCore
    {
        protected RenderWindow renderWindow;
        protected UserInput input;
        protected CameraManager cameraManager;

        protected SharpGL.OpenGL Gl
        {
            get { return renderWindow.Gl; }
        }

        private GameTime gameTime = new GameTime();
        private long startTime = 0;
        private long interval = 0;

        private static double FixedFPS = 60.0f;

        public GameCore()
        {
            Console.ForegroundColor = ConsoleColor.DarkGreen;
            Console.WriteLine("[" + gameTime.ElapsedTimeString + "] " + "Game Application Started");
            Initialize();

            /*Console.WriteLine("[" + gameTime.ElapsedTimeString + "] " + "Loading 100 teapots");
            for(int i=0;i<100;i++)
                ContentLoader.LoadMesh("Models/teapot.obj");
            Console.WriteLine("[" + gameTime.ElapsedTimeString + "] " + "Loading done!");
            Console.ReadKey();*/
        }

        private void OpenRenderWindow()
        {
            renderWindow = new RenderWindow();
            renderWindow.SetIcon(ContentLoader.LoadIcon("Images/Icon.ico"));
            Application.Run(renderWindow);
        }

        private void EngineUpdate()
        {
            int i = 0;
            while (i == 0)
            {
                if (gameTime.TotalTime.ElapsedMilliseconds - startTime >= interval)
                {
                    startTime = gameTime.TotalTime.ElapsedMilliseconds;
                    Update(gameTime);
                    Draw(gameTime);
                    gameTime.Update();
                }
            }
        }

        protected virtual void Initialize()
        {
            Console.ForegroundColor = ConsoleColor.DarkGray;
            Console.WriteLine("[" + gameTime.ElapsedTimeString + "] " + "Starting Render Window");

            Console.Title = "Mutsi Engine Console Window";

            startTime = gameTime.TotalTime.ElapsedMilliseconds; ;
            //Calculate a interval for the update to be called
            interval = (long)TimeSpan.FromSeconds(1.0 / FixedFPS).TotalMilliseconds;

            //Open the RenderWindow
            renderWindow = new RenderWindow();
            renderWindow.Show();

            //Set OpenGL settings
            Gl.Enable(SharpGL.OpenGL.GL_CULL_FACE);
            Gl.Enable(SharpGL.OpenGL.GL_ALPHA_TEST);
            Gl.BlendFunc(SharpGL.OpenGL.GL_SRC_ALPHA, SharpGL.OpenGL.GL_ONE_MINUS_SRC_ALPHA);
            Gl.Enable(SharpGL.OpenGL.GL_BLEND);

            Gl.CullFace(SharpGL.OpenGL.GL_FRONT);

            //Create the userinput
            input = new UserInput(renderWindow);
            //Create the managers
            cameraManager = new CameraManager();

            Console.ForegroundColor = ConsoleColor.DarkGreen;
            Console.WriteLine("[" + gameTime.ElapsedTimeString + "] " + "Render Window Started");
        }

        protected virtual void LoadContent()
        {
            EngineUpdate();
        }

        protected virtual void Update(GameTime gameTime)
        {
            input.Update();
        }

        protected virtual void Draw(GameTime gameTime)
        {
            Application.DoEvents();
            renderWindow.Controls["openGLControl"].Refresh();
        }

        protected virtual void ClearScreen(Vector4 color)
        {
            Gl.Clear(SharpGL.OpenGL.GL_COLOR_BUFFER_BIT | SharpGL.OpenGL.GL_DEPTH_BUFFER_BIT);
            Gl.ClearColor(color.X, color.Y, color.Z, color.W);
        }

        protected void SetRenderWindowResolution(int width, int height)
        {
            renderWindow.SetResolution(width, height);
        }
    }
}
